#!/bin/bash
BR_CONF_DIR="/root/xen/bridge_config_dir"
VM_OPS_SCRIPTS_DIR="/root/xen/vm_ops_scripts"
source $VM_OPS_SCRIPTS_DIR/utils/log.bash 
if [ $# != 3 ]; then
   LOG_MSG "$* parameter error";
   return 255
fi
BRIDGE_ID=$1
IP=$2
MASK=$3
counter=0
function set_value(){
local name=$1
if [ $name == "gateway" ]; then
      export GATEWAY=$2
fi
if [ $1 == "ip_from" ]; then
      export IP_FROM=$2
fi
if [ $1 == "ip_to" ]; then
      export  IP_TO=$2
fi
}

#$VM_OPS_SCRIPTS_DIR/bridge/info_trans.out $MASK $IP

cat $VM_OPS_SCRIPTS_DIR/bridge/info.txt | while read name value
do
	set_value $name $value
	counter=`expr $counter + 1`
	if [ $counter -eq 3 ];then
cat <<EOF >$BR_CONF_DIR/br_$BRIDGE_ID.xml
<network>
<name>net_$BRIDGE_ID</name>
<forward mode='nat'/>
<bridge name="virbr_$BRIDGE_ID" stp='off' delay='0' />
<ip address="$GATEWAY" netmask="$MASK">
</ip>
</network>
EOF
		break;
        fi

done
#<dhcp>
#<range start="$IP_FROM" end="$IP_TO" />
#</dhcp>

virsh net-create $BR_CONF_DIR/br_$BRIDGE_ID.xml
if [ $? -ne 0 ]; then
	LOG_MSG "create bridge failed"
	exit 255;
fi

	


   
